package andriod.action.user;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import andriod.service.ArdDailyService;
import andriod.service.ArdDocService;
import andriod.service.ArdFlowService;
import andriod.service.ArdInfoService;
import andriod.service.ArdMailService;
import andriod.service.ArdTaskService;
import andriod.service.ArdUserService;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.OAddressbook;
import com.jcrj.entity.OOrg;
import com.jcrj.entity.ORole;
import com.jcrj.entity.OUser;
import com.jcrj.listener.UserInfo;
import com.jcrj.listener.UserListener;
import com.jcrj.service.InfoManagerService;
import com.jcrj.service.SysLogService;
import com.jcrj.service.TaskService;
import com.jcrj.service.UserService;
import com.jcrj.service.WorkflowManagerService;
import com.jcrj.util.DateProcessor;

public class ArdUserAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private String name;
	private String password;
	private int orgId;
	private UserService userService;
	private ArdUserService ardUserService;
	private SysLogService sysLogService;
	private ArdMailService ardMailService;
	private ArdDailyService ardDailyService;
	private ArdInfoService ardInfoService;
	private ArdTaskService ardTaskService;
	private WorkflowManagerService workflowManagerService;
	private ArdDocService ardDocService;
	private TaskService taskService;
	private ArdFlowService ardFlowService;
	private InfoManagerService infoManagerService;

	public String login() {
		try {
			OUser user = this.userService.login(this.name, this.password);
			JSONObject obj = new JSONObject();
			if (user != null) {
				List<ORole> list = this.userService.findRoleByUser(user.getId());
				if ((list != null) && (!list.isEmpty())) {
					user.setOrgName(this.userService.getUserAllOrgName(user.getOrgId().intValue()));
					UserInfo info = UserInfo.getIntance();
					UserListener ul = new UserListener();
					ul.setUserInfo(user);
					info.addUser(ul.getUserInfo());
					super.getSession().put("user", ul);
					super.getSession().put("userInfo", user);
					obj.put("userInfo", user);
					obj.put("JSESSIONID", super.getRequest().getSession().getId());
				} else {
					obj.put("errorInfo", "该用户没有授权！");
				}
			} else {
				obj.put("errorInfo", "用户名或密码错误！");
			}
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String loginOut() {
		try {
			super.getSession().clear();
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String loginClose() {
		try {
			super.getSession().clear();
			super.getResponse().getWriter().write("");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String mainHome() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			JSONObject obj = new JSONObject();
			String userId = this.sysLogService.getAgentUser(user);
			long count = this.ardMailService.countDraftMail(user.getId().intValue()) + this.ardMailService.countInMail(userId)
					+ this.ardMailService.countSendMail(user.getId().intValue()) + this.ardMailService.countDeleteMail(user.getId().intValue());
			obj.put("mailCount", Long.valueOf(count));
			count = this.ardDailyService.countDaily(user.getId().intValue());
			obj.put("dailyCount", Long.valueOf(count));
			String orgIds = this.infoManagerService.getUserAllOrgs(user.getOrgId().intValue());
			boolean role = this.infoManagerService.getRoleByUser(user.getId().intValue());
			count = this.ardInfoService.countInfo() + this.ardInfoService.countNotice(orgIds, role);
			obj.put("infoCount", Long.valueOf(count));
			count = this.ardTaskService.countTodoTask(userId) + this.ardTaskService.countUserTask(user.getId().intValue())
					+ this.ardTaskService.countNotifyTask(user.getId().intValue());
			obj.put("taskCount", Long.valueOf(count));
			List<Integer> agents = this.sysLogService.getFlowAgentUser(user);
			String processId = this.workflowManagerService.getProcessinstanceList(user.getId().intValue(), agents);
			count = this.ardDocService.countDoc(processId, "2");
			processId = this.workflowManagerService.getDoneProcessinstanceList(user.getId().intValue());
			count += this.ardDocService.countDoc(processId, "2");
			agents = this.sysLogService.getFlowAgentUser(user);
			processId = this.workflowManagerService.getProcessinstanceList(user.getId().intValue(), agents);
			count += this.ardDocService.countDoc(processId, "1");
			processId = this.workflowManagerService.getDoneProcessinstanceList(user.getId().intValue());
			count += this.ardDocService.countDoc(processId, "1");
			obj.put("docCount", Long.valueOf(count));
			agents = this.taskService.getAgentUser(user);
			String process = this.workflowManagerService.getSqlProcessinstanceList(user.getId().intValue(), agents);
			count = this.ardFlowService.countWorkFlowTaskFile(process, null).longValue();
			process = this.workflowManagerService.getSqlDoneProcessinstanceList(user.getId().intValue());
			count += this.ardFlowService.countWorkFlowTaskFile(process, null).longValue();
			agents = this.taskService.getAgentUser(user);
			process = this.workflowManagerService.getSqlProcessinstanceList(user.getId().intValue(), agents);
			count += this.ardFlowService.countWorkFlowTaskFile(process, user).longValue();
			obj.put("flowCount", Long.valueOf(count));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String bookHome() {
		try {
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getUserList() {
		try {
			List<OUser> userList = this.ardUserService.getUserList(this.name, this.orgId, getCurrentPage(), 20);
			for (OUser user : userList) {
				user.setOrgName(this.userService.getUserAllOrgName(user.getOrgId().intValue()));
			}
			long count = this.ardUserService.getUserCount(this.name, this.orgId);
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("list", userList);
			map.put("pageCount", Long.valueOf(count % 20L == 0L ? count / 20L : count / 20L + 1L));
			map.put("totalCount", count);
			map.put("currentPage", getCurrentPage());
			JSONObject obj = JSONObject.fromObject(map, config);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getBookUserList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			List<OAddressbook> list = this.ardUserService.getBookUserList(this.name, user.getId().intValue(), getCurrentPage(), 20);
			long count = this.ardUserService.getBookUserCount(this.name, user.getId().intValue());
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("list", list);
			map.put("total", Long.valueOf(count / 20L == 0L ? 0L : count / 20L + 1L));
			JSONObject obj = JSONObject.fromObject(map, config);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getOrgList() {
		try {
			List<OOrg> orgList = this.ardUserService.getOrgList();
			for (OOrg org : orgList) {
				org.setName(this.userService.getUserAllOrgName(org.getId().intValue()));
			}
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			JSONArray obj = JSONArray.fromObject(orgList, config);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getOrgId() {
		return this.orgId;
	}

	public void setOrgId(int orgId) {
		this.orgId = orgId;
	}

	public UserService getUserService() {
		return this.userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ArdUserService getArdUserService() {
		return this.ardUserService;
	}

	public void setArdUserService(ArdUserService ardUserService) {
		this.ardUserService = ardUserService;
	}

	public SysLogService getSysLogService() {
		return this.sysLogService;
	}

	public void setSysLogService(SysLogService sysLogService) {
		this.sysLogService = sysLogService;
	}

	public ArdMailService getArdMailService() {
		return this.ardMailService;
	}

	public void setArdMailService(ArdMailService ardMailService) {
		this.ardMailService = ardMailService;
	}

	public ArdDailyService getArdDailyService() {
		return this.ardDailyService;
	}

	public void setArdDailyService(ArdDailyService ardDailyService) {
		this.ardDailyService = ardDailyService;
	}

	public ArdInfoService getArdInfoService() {
		return this.ardInfoService;
	}

	public void setArdInfoService(ArdInfoService ardInfoService) {
		this.ardInfoService = ardInfoService;
	}

	public ArdTaskService getArdTaskService() {
		return this.ardTaskService;
	}

	public void setArdTaskService(ArdTaskService ardTaskService) {
		this.ardTaskService = ardTaskService;
	}

	public WorkflowManagerService getWorkflowManagerService() {
		return this.workflowManagerService;
	}

	public void setWorkflowManagerService(WorkflowManagerService workflowManagerService) {
		this.workflowManagerService = workflowManagerService;
	}

	public ArdDocService getArdDocService() {
		return this.ardDocService;
	}

	public void setArdDocService(ArdDocService ardDocService) {
		this.ardDocService = ardDocService;
	}

	public TaskService getTaskService() {
		return this.taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public ArdFlowService getArdFlowService() {
		return this.ardFlowService;
	}

	public void setArdFlowService(ArdFlowService ardFlowService) {
		this.ardFlowService = ardFlowService;
	}

	public InfoManagerService getInfoManagerService() {
		return this.infoManagerService;
	}

	public void setInfoManagerService(InfoManagerService infoManagerService) {
		this.infoManagerService = infoManagerService;
	}
}